<!--
列表项组件
v1.0.1 移除col，more项，自动宽度，支持图标项
v1.0.0
-->

<template>
  <div class="sh-list-item"
       @tap.stop="bindClick()"
       :class="{'arrow-item':type=='arrow','more-item':type=='more','checkbox-item':type=='checkbox','switch-item':type=='switch'}">
    <div class="sh-list-item-inner">
      <div class="sh-list-item-left-icon" v-if="$slots['left-icon']">
        <slot name="left-icon"></slot>
      </div>
      <div class="sh-list-item-content sh-left"
           v-if="$slots.left"
           :style="left_style">
        <slot name="left"></slot>
      </div>
      <div class="sh-list-item-content sh-mid"
           v-if="$slots.mid"
           :style="mid_style">
        <slot name="mid"></slot>
      </div>
      <div class="sh-list-item-content sh-right"
           v-if="$slots.right"
           :style="right_style">
        <slot name="right"></slot>
      </div>
      <div class="sh-list-item-right-icon" v-if="$slots['right-icon']">
        <slot name="right-icon"></slot>
      </div>

      <div class="sh-list-item-right-mark" v-if="type">
        <div class="sh-icon-checkbox" :class="{active:this_checked,disabled:this_disabled}"
             v-if="type=='checkbox' || type=='radio'">
        </div>
        <div class="sh-icon-switch" :class="{active:this_checked,disabled:this_disabled}" v-if="type=='switch'">
        </div>
        <div class="sh-icon-arrow" v-if="type=='arrow'">
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'sh-list-item',
    props: {
      value: {//输出值
        default: false,
        require: false,
      },
      type: String,
      scale: {//比例
        type: Array,
        default: function () {
          return [1, 1]
        },
        require: false,
      },
      /*col: {//列数
        type: Number,
        default: 2,
        require: false,
      },*/
      label: [String, Boolean],//当前值
      is_show_sub: {
        default: false,
        require: false,
      },
      disabled: String,
    },
    data() {
      return {
        this_checked: false,
        this_show_sub: false,
        this_scale: [],
        this_disabled: false,

      }
    },
    beforeMount: function () {

    },
    updated: function () {
      this.sync();
    },
    mounted: function () {
//      console.log(this.$slots);
      if (this.is_show_sub == "true") {
        this.this_show_sub = true;
      }
      this.this_scale = this.scale;
      /*if (this.col == 3 && this.scale.length == 2) {
        this.this_scale = [this.this_scale[0], 1, this.this_scale[1]];
      }*/

      if (this.disabled !== undefined) {
        this.this_disabled = true;
      }
      this.sync();
    },
    methods: {
      start() {
        this.isMoving = false;
      },
      move() {
        this.isMoving = true;
      },
      sync() {
        if (this.value == this.label) {
          this.this_checked = true;
        } else {
          this.this_checked = false;
        }
      },
      bindClick() {
        if (!this.this_disabled) {
          if (this.type == 'checkbox' || this.type == 'switch') {
            this.this_checked = !this.this_checked;
            if (this.this_checked) {
              this.$emit('input', (typeof this.label === 'boolean') ? true : this.label);
            } else {
              this.$emit('input', (typeof this.label === 'boolean') ? false : '');
            }

          }
          if (this.type == 'radio') {
            this.this_checked = true;
            if (this.this_checked) {
              this.$emit('input', this.label);
            } else {
              this.$emit('input', '');
            }

          }
        } else {
          this.this_checked = false;
          this.$emit('input', '');
        }

        if (this.type == 'more') {
          this.this_show_sub = !this.this_show_sub;
        }
        this.$emit('tap');
      }
    },
    computed: {
      left_style() {
        let scale = this.this_scale[0] || 1;
        if (scale === 'auto') {
          scale = '0 1 auto';
        }
        return `flex:${scale};-webkit-flex:${scale}`;
      },
      mid_style() {
        let scale = this.this_scale[1] || 1;
        if (scale === 'auto') {
          scale = '0 1 auto';
        }
        return `flex:${scale};-webkit-flex:${scale}`;
      },
      right_style() {
        let scale = (this.this_scale && this.this_scale.length > 0) ? this.this_scale[this.this_scale.length - 1] : 1;
        if (scale === 'auto') {
          scale = '0 1 auto';
        }
        return `flex:${scale};-webkit-flex:${scale}`;
      },
    },
    watch: {},
  };
</script>
<style>
  .sh-list-item.arrow:active {
    background: #eee;
  }

  .sh-list-item-inner {
    margin-left: 1rem;
    padding-right: 1rem;
    border-bottom: 1px solid #ebebeb;
    display: flex;
    display: -webkit-flex;
    align-items: center;
    -webkit-align-items: center;
    flex-wrap: wrap;
    -webkit-flex-wrap: wrap;
  }

  .sh-list-item:last-child .sh-list-item-inner {
    border-bottom: 0;
  }

  .sh-list-item-content {
    padding: 1rem 0;
  }

  .sh-list-item-left-icon {
    margin-right: 0.5rem;
  }

  .sh-list-item-right-icon {
    margin-left: 0.5rem;
  }

  .sh-list-item-left-icon img {
    display: block;
  }

  .sh-list-item-right-icon img {
    display: block;
  }

  .sh-list-item-right-mark {
    /*width: 1.5rem;*/
    margin-left: 0.5rem;
    text-align: right;
  }

  .sh-left {
    color: #666666;
    flex: 1;
    -webkit-flex: 1;
  }

  .sh-mid {
    text-align: center;
    flex: 1;
    -webkit-flex: 1;
  }

  .sh-right {
    text-align: right;
    flex: 1;
    -webkit-flex: 1;
  }

  .sh-right input {
    text-align: right;
  }

  .sh-icon-checkbox {
    position: relative;
    text-align: center;
    width: 0.8rem;
    display: flex;
    display: -webkit-flex;
    justify-content: center;
    -webkit-justify-content: center;
    align-items: center;
    -webkit-align-items: center;
  }

  .sh-icon-checkbox:after {
    content: '';
    display: inline-block;
    padding: 0.5rem;
    border: 1px solid #ccc;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    border-radius: 50%;
    vertical-align: middle;
  }

  .sh-icon-checkbox.active:after {
    border: 1px solid #28bc93;
    background: #28bc93;
  }

  .sh-icon-checkbox.active:before {
    content: '';
    display: inline-block;
    position: absolute;
    width: 0.2rem;
    height: 0.4rem;
    border: 1px solid #fff;
    border-bottom: 1px solid transparent;
    border-right: 1px solid transparent;
    top: -2px;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto auto;

    transform: rotate(-135deg);
    -ms-transform: rotate(-135deg); /* IE 9 */
    -moz-transform: rotate(-135deg); /* Firefox */
    -webkit-transform: rotate(-135deg); /* Safari 和 Chrome */
    -o-transform: rotate(-135deg); /* Opera */
  }

  .sh-icon-switch {
    width: 1.5rem;
    height: 1rem;
    background: #ccc;
    -webkit-border-radius: 1rem;
    -moz-border-radius: 1rem;
    border-radius: 1rem;
    position: relative;
    overflow: hidden;
  }

  .sh-icon-switch:before {
    content: '';
    display: block;
    border: 0 solid #28bc93;
    padding: 0.5rem;
    -webkit-transition: all 0.1s;
    -moz-transition: all 0.1s;
    -ms-transition: all 0.1s;
    -o-transition: all 0.1s;
    transition: all 0.1s;
    vertical-align: middle;
    -webkit-border-radius: 1rem;
    -moz-border-radius: 1rem;
    border-radius: 1rem;
  }

  .sh-icon-switch:after {
    content: '';
    display: inline-block;
    position: absolute;
    /*padding:0.4rem;*/
    height: 0.8rem;
    width: 0.8rem;
    -webkit-border-radius: 1rem;
    -moz-border-radius: 1rem;
    border-radius: 1rem;
    background: #fff;
    top: 0;
    bottom: 0;
    right: 0.5rem;
    left: 0;
    margin: auto auto;
    box-sizing: border-box;

    transition: right 0.2s;
    -moz-transition: right 0.2s; /* Firefox 4 */
    -webkit-transition: right 0.2s; /* Safari 和 Chrome */
    -o-transition: right 0.2s; /* Opera */
  }

  .sh-icon-switch.active:before {
    border: 0.5rem solid #28bc93;
    padding: 0;
  }

  .sh-icon-switch.active:after {
    right: -0.5rem;
  }

  .sh-icon-arrow {
    display: flex;
    display: -webkit-flex;
    align-items: center;
    -webkit-align-items: center;
  }

  .sh-icon-arrow:after {
    content: '';
    border: 1px solid #666;
    border-right: 1px solid transparent;
    border-bottom: 1px solid transparent;
    padding: 0.2rem;
    display: inline-block;
    /*vertical-align: middle;*/
    -webkit-transform: rotate(135deg);
    -moz-transform: rotate(135deg);
    -ms-transform: rotate(135deg);
    -o-transform: rotate(135deg);
    transform: rotate(135deg);
  }

  .sh-icon-switch.disabled {
    -webkit-filter: brightness(1.15);
    filter: brightness(1.15);
  }

  .sh-icon-checkbox.disabled {
    /*-webkit-filter:brightness(1.15);*/
    /*filter:brightness(1.15);*/
  }

  .sh-icon-checkbox.disabled:after {
    background: #cccccc;
  }

  .sh-icon-checkbox.disabled:before {
    content: '×';
    display: inline-block;
    position: absolute;
    width: 0.8rem;
    height: 0.8rem;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto auto;
    color: #ffffff;
    line-height: 100%;
  }
</style>
